package com.hngm.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hngm.bean.Company;
import com.hngm.bean.Productpicture;
import com.hngm.util.DBManager;
import com.hngm.util.QueryDataResult;

public class ProductpictureDao {
    public ArrayList<Productpicture> queryProductpictureList(){
    	//定义图片集合
    	ArrayList<Productpicture> ppList = new ArrayList<Productpicture>();
    	//定义sql语句
    	String sql = "select * from productpicture";
  	   //调用DBManager查询数据库
  	   QueryDataResult qdr = DBManager.executeQuery(sql);
  	   //从qdr中获取resultSet结果集
  	   ResultSet rs = qdr.getRs();
  	   
  		   //遍历结果集rs
  		try {
			while(rs.next()){
				long id = rs.getLong("id");
				String picurl = rs.getString("picurl");
				long product_id = rs.getLong("product_id");
				int del = rs.getInt("del");
				String creationtime = rs.getString("creationtime");
				String updatetime = rs.getString("updatetime");
				Productpicture pp = new Productpicture(id, picurl, product_id, del, creationtime, updatetime);
				ppList.add(pp);
			   }
		} catch (SQLException e) {
			DBManager.closedAll(qdr.getConn(), qdr.getSt(), qdr.getRs());
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			DBManager.closedAll(qdr.getConn(), qdr.getSt(), qdr.getRs());
		}
    	return ppList;
    }
    //根据产品id查询产品列表
    public List<Productpicture> queryPicListByPid(String productId){
    	List<Productpicture> picList = new ArrayList<Productpicture>();
    	//定义查询sql语句
    	String sql = "select * from productpicture where product_id = " + productId;
    	QueryDataResult qdr = DBManager.executeQuery(sql);
    	//获取结果集
    	ResultSet rs = qdr.getRs();
    	//遍历结果集
    	try {
			while(rs.next()){
				long id = rs.getLong("id");
				String picurl = rs.getString("picurl");
				long product_id = rs.getLong("product_id");
				Productpicture p = new Productpicture();
				p.setId(id);
				p.setPicurl(picurl);
				p.setProduct_id(product_id);
				picList.add(p);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			DBManager.closedAll(qdr.getConn(), qdr.getSt(), qdr.getRs());
		}
    	return picList;
    }
    //插入产品图片的方法
	public int insertPropicture(Productpicture pic) {
		String sql = String.format("insert into productpicture (picurl,product_id) values ('%s',%d)", pic.getPicurl(),pic.getProduct_id());
		return DBManager.executeUpdate(sql);
	}
	public int delProductpiture(String productId) {
		//定义删除产品图片的sql语句
		String sql = "delete from productpicture where id = " + productId;
		return DBManager.executeUpdate(sql);
	}
}

